#include <windows.h>
#include <commctrl.h>
#include "resource.h"

// Date/Time subclass proc.
LRESULT CALLBACK NewDTProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	RECT rect;
	HDC hdc;

	WNDPROC OldDTProc = (WNDPROC)GetWindowLong(hwnd, GWL_USERDATA);
	
	switch(message)
	{
	case WM_ERASEBKGND:
		hdc = (HDC)wParam;
		GetClientRect(hwnd, &rect);
		FillRect(hdc, &rect, GetSysColorBrush(COLOR_BTNHILIGHT));
		return -1;

	case WM_NCPAINT:
		hdc = GetWindowDC(hwnd);

		//work out where the window border is
		GetWindowRect(hwnd, &rect);
		OffsetRect(&rect, -rect.left, -rect.top);
		InflateRect(&rect, -1, -1);

		//draw a 1-pixel wide line around the outside
		FrameRect(hdc, &rect, GetSysColorBrush(COLOR_3DSHADOW));

		ReleaseDC(hwnd, hdc);

		return 0;

	case WM_SIZE:
		return 0;

	default:
		break;
	}
	
	return CallWindowProc(OldDTProc, hwnd, message, wParam, lParam);
}

int MakeCoolDateTime(HWND hwndDT)
{
	RECT    rect;
	WNDPROC oldproc;
	
	//work out how big the date-time control is.
	GetWindowRect(hwndDT, &rect);
	OffsetRect(&rect, -rect.left, -rect.top);

	//resize the date-time control horizontally. We don't
	//want to move it, so we can use SetWindowPos.
	SetWindowPos(hwndDT, 0, 0, 0, rect.right+100, rect.bottom, 
		SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);

	//subclass the control so when we size it back to what it was,
	//our wndproc will intersept the WM_SIZE, and the scrollbar will not
	//get shifted back into the control
	oldproc = (WNDPROC)SetWindowLong(hwndDT, GWL_WNDPROC, (LONG)NewDTProc);

	//we need to remember the old window procedure. Just store
	//the pointer in the USERDATA area of the window.
	SetWindowLong(hwndDT, GWL_USERDATA, (LONG)oldproc);

	//make it a little smaller too
	rect.right -= 2;
	rect.bottom -= 2;

	//size the date-time control back to it's original place
	SetWindowPos(hwndDT, 0, 0, 0, rect.right, rect.bottom, 
		SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);


	return 0;
}

BOOL CALLBACK DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_INITDIALOG:

		MakeCoolDateTime(GetDlgItem(hwnd, IDC_DATETIMEPICKER1)); //created date
		MakeCoolDateTime(GetDlgItem(hwnd, IDC_DATETIMEPICKER2)); //created time
		MakeCoolDateTime(GetDlgItem(hwnd, IDC_DATETIMEPICKER3)); //modified date
		MakeCoolDateTime(GetDlgItem(hwnd, IDC_DATETIMEPICKER4)); //modified time
		MakeCoolDateTime(GetDlgItem(hwnd, IDC_DATETIMEPICKER5)); //accessed date
		MakeCoolDateTime(GetDlgItem(hwnd, IDC_DATETIMEPICKER6)); //accessed time

		return TRUE;

	case WM_COMMAND:
		
		switch(LOWORD(wParam))
		{
		case IDOK: case IDCANCEL:
			EndDialog(hwnd, 0);
			return TRUE;
		}
		
		return FALSE;

	case WM_CLOSE:
		EndDialog(hwnd, 0);
		return TRUE;

	}
	return FALSE;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd)
{
	INITCOMMONCONTROLSEX ice;
	
	ice.dwSize = sizeof(ice);
	ice.dwICC  = ICC_DATE_CLASSES;

	//our dialog won't display if the date-time control class is
	//not registered. 
	InitCommonControlsEx(&ice);

	return DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), 0, DlgProc);
}